package com.mindtree.vi.business.aspect.validation;

import java.lang.reflect.Field;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

import com.mindtree.vi.dbaccess.datatypes.Datatype;

/**
 * This aspect checks for the input parameters for the pattern and nullity
 * 
 * @author m1005309
 * 
 */
@Aspect
@Order(100)
public class ValidationAspect {

	private boolean success = true;// this is a catch because aspect will be
	// singleton

	private ResourceBundle bundle;// this is a catch because aspect will be

	// singleton

	@AfterReturning(pointcut = "within (* com.mindtree.vi.dbaccess.manager..*(..) )", returning = "datatype")
	public Object validateAfter(ProceedingJoinPoint joinpoint, Datatype datatype) {

		System.out.println("AfterReturning!!! the datatype value is + "
				+ datatype);
		Object[] arguments = joinpoint.getArgs();
		System.out.println(joinpoint.getTarget().getClass());
		for (Object object : arguments) {
			System.out.println("Validating " + object);
			if (object == null) {

			}
		}
		return false;
	}

	@Before(value = "execution (* com.mindtree.vi.business..*.*(..)) && args(problem) )")
	public Object validateBeforeQuestionSubmission(
			ProceedingJoinPoint joinpoint, Datatype problem) throws Throwable {
		System.out.println("Validating Datatype-->!!! " + problem);
		Field[] fields = problem.getClass().getDeclaredFields();
		String type = problem.getClass().getSimpleName();
		type = type.replaceFirst("" + type.charAt(0), ""
				+ Character.toLowerCase(type.charAt(0)));
		for (Field field : fields) {
			if (field != null) {
				doChecks(type, field, problem);
			}
		}

		return success;
	}

	private void doChecks(String type, Field field, Object obj)
			throws Throwable {
		System.out.println("Master Validation is ON!");
		for (ValidationType validationType : ValidationType.values()) {
			String bundleKey = "";
			try {
				bundle = ResourceBundle.getBundle(type + "-"
						+ validationType.getStringValue() + "-" + "validation");
				bundleKey = bundle.getString(field.getName());
				
				/* if not configured it will throw MissingResourceException */
				
				field.setAccessible(true);
				Object object = field.get(obj);
				switch (validationType) {
				case NOT_NULL:
					if (object == null) {
						throw new ValidationException(field.getName());
					}
					break;
				case LENGTH:
					if (field.getType().equals(String.class)) {
						String string = (String) field.get(obj);
						if (string.length() > Integer.parseInt(bundleKey)) {
							throw new ValidationException();
						}
					}
					break;
				case PATTERN: 
					/*
					 * TODO: Need to implement PATTERN based validation yet!
					 */
					break;
				}

			} catch (MissingResourceException e) {
				// Validation Passed, nothing configured hence passed.
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			}
		}
		return;

	}

}
